package pdf;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPrintable;
import org.apache.pdfbox.printing.Scaling;

import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.PageRanges;
import javax.print.attribute.standard.Sides;
import java.io.File;
import java.io.IOException;

/**
 * 内网打印模块
 */
public class PDFPrinter {

    public static void main(String[] args) throws IOException {
        //内网电脑 文件打印位置
        File folder = new File("D:\\work\\临时打印");
        rangeFiles(folder);

    }

    //遍历文件夹
    public static void rangeFiles(File folder) throws IOException {
        File[] listOfFiles = folder.listFiles();

        if (listOfFiles != null){
            for (File file: listOfFiles){

                if (file.isDirectory()){
                    //如果是目录就递归调用
                    rangeFiles(file);

                } else if(file.isFile() && file.getName().endsWith(".pdf")){
                    //如果是pdf就打印
                    System.out.println("打印文件:" + file.getName());
                    printPDF(file);
                }

            }
        }
    }

    public static void printPDF(File file) throws IOException {
        // 1. 使用 Apache PDFBox 加载 PDF 文件
        PDDocument document = PDDocument.load(file);

        // 2. 获取默认的打印服务
        PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
        PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
        //装完打印机驱动后需要检查  3为368   4为286
        printService = printServices[3];
        if (printService == null) {
            System.out.println("没有找到默认的打印机！");
            return;
        }

        // 3. 创建 PrinterJob
        PrinterJob printerJob = PrinterJob.getPrinterJob();
        try {
            printerJob.setPrintService(printService);
        } catch (PrinterException e) {
            throw new RuntimeException(e);
        }

        // 4. 获取打印机的页面格式
        PageFormat pageFormat = printerJob.defaultPage();
        Paper paper = pageFormat.getPaper();

        // 设置A4纸的大小
        paper.setSize(595, 842); // A4纸的尺寸，单位是点（1 inch = 72 points）
        paper.setImageableArea(0, 0, 595, 842); // 设置可打印区域
        // 更新页面格式
        pageFormat.setPaper(paper);

        //创建 PDFPrintable 对象
        PDFPrintable pdfPrintable = new PDFPrintable(document, Scaling.SHRINK_TO_FIT);
        //设置页面格式并应用到打印任务
        printerJob.setPrintable(pdfPrintable, pageFormat);


        // 5. 设置打印属性
        PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
        attributes.add(Sides.ONE_SIDED); // 设置单面打印
        attributes.add(new PageRanges(1, document.getNumberOfPages())); // 打印所有页面
        attributes.add(new Copies(2)); // 打印2份

        // 6. 开始打印
        try {
            printerJob.print(attributes);
            System.out.println("打印成功!");
        } catch (Exception e) {
            System.out.println("打印失败: " + e.getMessage());
        } finally {
            document.close();
        }
    }
}
